console.log('tax injected');
// import SH from './../commonModule/SH.js';
import { autoInputAccount } from './option/autoInputAccount.js';
import { fillHsqj } from './option/hsqj.js';
import { fillCwbb, btnclickEvent } from './option/fillCwbb.js';
import { getNationData } from './utils.js';

// 获得基本信息
if (window.$ && $(window)) {
  $(window).load(() => {
    // 获得基本信息
    window.postMessage(
      {
        event: 'getBaseInfo'
      },
      '*'
    );
  });
} else {
  setTimeout(() => {
    // 获得基本信息
    window.postMessage(
      {
        event: 'getBaseInfo'
      },
      '*'
    );
  }, 3000);
}

// 监听事件
window.addEventListener('message', async function ({ data }) {
  // 只监听来自于content script的信息
  if (data.sender !== 'content-script') {
    return false;
  }
  
  if (data.event === 'setSaasData') {
    data = data.data;
    let sheet = data.saasData.sheet;
    if (sheet == 'sds' || sheet == 'zzs') {
      // fillInput(data, sheet);
    } else if (sheet.includes('hsqj')) {
      fillHsqj(data);
    } else {
      fillCwbb(data, sheet);
    }
  }

  if (data.event === 'getNationData') {
    getNationData();
  }

  if (data.event === 'getBaseInfo') {
    const companyInfoStr = JSON.stringify(data.data);
    // 将数据存储到localStorage(sessionStorage只能保存同一窗口,税局会打开新的窗口)
    // 先清除本地储存(避免数据受到污染)
    window.localStorage.removeItem('companyInfo');
    window.localStorage.setItem('companyInfo', companyInfoStr);
    autoInputAccount(data.data);

    // 小企业会计准则页面处理
    if (window.location.href.includes('sb_cwbb_xqy_kjzz/sb_cwbb_xqy_kjzz.html')) {
      // 监听tab点击事件
      btnclickEvent()
    }
  }
}, false);
